using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Threading;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.StorageClient;
using System.Configuration;
using System.Data.Sql;
using System.Data.SqlClient;

namespace DMVSnapShot
{
    public class WorkerRole : RoleEntryPoint
    {
        public enum UpdateType {UpdateExecStats }
        public override void Run()
        {
            
            Trace.WriteLine("DMVSnapShot entry point called", "Information");
            double TimespanMins = 5.0;

            while (true)
            {
                UpdateDMV(UpdateType.UpdateExecStats,ConfigurationManager.ConnectionStrings["licensesimDB"].ConnectionString);
                Thread.Sleep((int)(TimespanMins*60*1000));
                Trace.WriteLine("Working", "Information");
            }
        }
        public void UpdateDMV(UpdateType u, string connString) 
        {
            if (u == UpdateType.UpdateExecStats)
            {
                using (SqlConnection sqlConn = new SqlConnection(connString))
                {

                    try
                    {
                        sqlConn.Open();
                        String cmdText = @"EXEC copyDMV";
                        SqlCommand cmd = new SqlCommand(cmdText, sqlConn);
                        cmd.ExecuteNonQuery();
                    }
                    catch (Exception e)
                    {
                        Trace.WriteLine("Couldn't copy DMV: " + e);
                    }
                }
            }
        }
        public override bool OnStart()
        {
            // Set the maximum number of concurrent connections 
            ServicePointManager.DefaultConnectionLimit = 12;

            // For information on handling configuration changes
            // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.

            TimeSpan tsOneMinute = TimeSpan.FromMinutes(1.0f);
            DiagnosticMonitorConfiguration dmc = DiagnosticMonitor.GetDefaultInitialConfiguration();
            dmc.Logs.ScheduledTransferPeriod = tsOneMinute;
            dmc.Logs.ScheduledTransferLogLevelFilter = LogLevel.Verbose;
            DiagnosticMonitor.Start("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString", dmc);
            RoleEnvironment.Changing += RoleEnvironmentChanging;

            return base.OnStart();
        }

        private void RoleEnvironmentChanging(object sender, RoleEnvironmentChangingEventArgs e)
        {
            // If a configuration setting is changing
            if (e.Changes.Any(change => change is RoleEnvironmentConfigurationSettingChange))
            {
                // Set e.Cancel to true to restart this role instance
                e.Cancel = true;
            }
        }
    }
}
